package algotithm.weekendrace.leetcode225.test3;

import java.util.Arrays;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/24-10:09
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] matrix = {{5,2},{1,6}};
        int k = 4;
        int kthLargestValue = solution.kthLargestValue(matrix, k);
        System.out.println(kthLargestValue);
    }

    public int kthLargestValue(int[][] matrix, int k) {
        int m = matrix.length;
        int n = matrix[0].length;
        int index = 0;
        int[] arr = new int[m*n];
        for (int i = 0; i < m; i++) {
            for (int  j = 0; j < n; j++) {
                if ( i > 0) {
                    matrix[i][j] ^= matrix[i-1][j];
                }
                if ( j > 0) {
                    matrix[i][j] ^= matrix[i][j-1];
                }
                if (i > 0 && j > 0) {
                    matrix[i][j] ^= matrix[i-1][j-1];
                }
                arr[index++] = matrix[i][j];
            }
        }
        Arrays.sort(arr);
        return arr[arr.length - k ];
    }
}


